﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bts_webapp.Models;
using Telerik.Web.UI;

namespace bts_webapp
{
    public partial class DeviceManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem)
            {
               
                UxDevice uxDevice = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxDevice;
                var DeviceUpdate = uxDevice.GetData();
                var device = this.DbContext.VIBA_THIET_BIs.FirstOrDefault(u => u.ThietBi_Id.ToLower() == DeviceUpdate.ThietBi_Id.ToLower());
                if (device != null)
                {
                    device.ThietBi_Id = DeviceUpdate.ThietBi_Id;
                    device.ThietBi_Ten = DeviceUpdate.ThietBi_Ten;
                    device.ThietBi_NhanHieu = DeviceUpdate.ThietBi_NhanHieu;
                    device.ThietBi_GhiChu = DeviceUpdate.ThietBi_GhiChu;
                    device.Tram_Id = DeviceUpdate.Tram_Id;
                    device.Status = DeviceUpdate.Status;

                    this.DbContext.SubmitChanges();
                }
                
            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_THIET_BIs.ToList();
           
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var DeviceId = (e.Item as GridDataItem)["ThietBi_Id"].Text;
                var device = this.DbContext.VIBA_THIET_BIs.FirstOrDefault(u => u.ThietBi_Id.ToLower() == DeviceId.ToLower());
                if (device != null)
                {
                    this.DbContext.VIBA_THIET_BIs.DeleteOnSubmit(device);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var device = e.Item.DataItem as VIBA_THIET_BI;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {
                
                UxDevice uxDevice = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxDevice;
                uxDevice.AddComboBoxItems();
                uxDevice.EnableFieldCode(true);
                uxDevice.InsertOrUpdate = true;
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UxDevice uxDevice = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxDevice;
                uxDevice.AddComboBoxItems();
                uxDevice.EnableFieldCode(false);
                uxDevice.BindData(device);
                uxDevice.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem)
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);
                dataItem["Tram_Ten"].Text = GetStationName(device.Tram_Id);
            }
        }

        public string GetStationName(string stationId)
        {
            if (stationId == null)
                stationId = "";
            var st = this.StationList.FirstOrDefault(x => x.Tram_Id.Trim() == stationId.Trim());
            if (st != null)
                return st.Tram_Ten;
            return string.Empty;
        }

        private List<VIBA_TRAM> _stationList;
        public List<VIBA_TRAM> StationList
        {
            get
            {
                if (_stationList == null)
                    _stationList = new BTSModelDataContext().VIBA_TRAMs.ToList();
                return _stationList;
            }
            set
            {
                _stationList = value;
            }
        }

        private string GetStatus(string status)
        {
            if (status == "1")
                return "<font color='blue'>Hoạt động</font>";
            return "<font color='red'>Không hoạt động</font>";
        }  

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxDevice uxDevice = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxDevice;
                var device = uxDevice.GetData();
                if (!IsExists(device.ThietBi_Id))
                {
                    this.DbContext.VIBA_THIET_BIs.InsertOnSubmit(device);
                    this.DbContext.SubmitChanges();
                    uxDevice.SetErrorStatus(string.Empty);
                }
                else
                {
                    uxDevice.SetErrorStatus("Xin lỗi, mã thiết bị đã tồn tại.");
                    e.Canceled = true;
                }
            }
        }

        private bool IsExists(string deviceId)
        {
            using (var model = new BTSModelDataContext())
            {
                var item = model.VIBA_THIET_BIs.FirstOrDefault(x => x.ThietBi_Id.ToLower() == deviceId.ToLower());
                return (item != null);
            }
        }
    }
}